<?php

class BlogParser
{
    const MAX_LINKS = 3;
    const SIDEBAR_CLASS_NAME = "LATEST_SIDEBAR";
    const RELATED_CACHE_DIR = "html/";
    const SIDEBARS_CACHE_DIR = "_sidebar_panels";
    
    /** 
     *  Gets content of Blog RSS file
     *  @return String the content of Rss file,
     */
    public static function GetBlogRssFile()
    {
        $data = "";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, RSS_LINK);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        
        $result = curl_exec($ch);
        if ( $result !== false )
        {
            $data = $result;
        }
        curl_close($ch);
        return $data;
    }
    
    /**
     * Parses xml blog file and puts fetched values to an array
     * @param string $content
     * @return array $result An array which consists of [Link to post] => [Title of the post].
     * May return empty array on failure.
     */
    public static function ParseRssBlogToArray($content)
    {
        $result = array();
        if ( !empty($content) )
        {
            libxml_use_internal_errors(true);
            $xmlObject = new SimpleXMLElement($content);
            if ( isset($xmlObject->channel->item) )
            {
                $linksCount = count($xmlObject->channel->item);
                $i = 0;
                foreach ($xmlObject->channel->item as $key)
                {
                    if ( $i < self::MAX_LINKS )
                    {
                        $result[(string) $key->link] = (string) $key->title;
                    }
                    $i++;                       
                }
            }
        }
        return $result;
    }

    /**
     * Checks if blog links from cache are Up-To-Date
     * @param $blogLinksArray An array which consists of [Link to post] => [Title of the post].
     * @return boolean True if links are Up-To-Date, false overwise 
     */
    public static function IsCacheUpToDate($blogLinksArray)
    {
        $jsonEncodedString = json_encode($blogLinksArray);
        $cacheString = @file_get_contents(SITE_LOCAL_PATH . BLOG_LINKS_CACHE_PATH);
        return ( $jsonEncodedString === $cacheString );
    }   

    /**
     * Writes data represented by $array into cache file
     * @param array $array
     * @return boolean True on success, false on failure
     */
    public static function WriteArrayToCache($array)
    {
        $result = file_put_contents(SITE_LOCAL_PATH . BLOG_LINKS_CACHE_PATH, json_encode($array));
        return $result !== false;
    }

    /**
     * Removes all cache files containing data of LATEST_SIDEBAR
     */	
    public static function RemoveOldRelatedCache()
    {
        $cacheObj = new FileCache(SITE_LOCAL_PATH . CACHE_DIR);
        $cacheObj->Remove(self::RELATED_CACHE_DIR, false);
        $cacheObj->SetDir(SITE_LOCAL_PATH . CACHE_DIR . self::RELATED_CACHE_DIR);

        $pages = Core::ResourceManager()->GetPages();        
        foreach( $pages as $value )
        {
            $cacheObj->Remove($value->id, false);
        }
        $cacheObj->SetDir(SITE_LOCAL_PATH . CACHE_DIR . self::RELATED_CACHE_DIR . self::SIDEBARS_CACHE_DIR);
        $cacheObj->Remove(self::SIDEBAR_CLASS_NAME);
    }
}

?>